package ru.spb.xmlreshelper.screen.database;

import com.google.common.collect.Maps;
import ru.spb.xmlreshelper.domain.ICdtService;
import ru.spb.xmlreshelper.gui.FieldLabelPair;
import ru.spb.xmlreshelper.gui.PanelEvent;
import ru.spb.xmlreshelper.util.GuiUtils;
import ru.spb.xmlreshelper.util.SpringUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author Nikita.Brazhnikov
 */
public class DatabaseConnectionPanel extends JPanel {

    private JPanel inputPanel;
    private JPanel testPanel;

    private FieldLabelPair url;
    private FieldLabelPair port;
    private FieldLabelPair sid;
    private FieldLabelPair username;
    private FieldLabelPair password;

    private JButton testBtn;
    private JLabel testResultLabel;

    private DatabaseConnectionController controller;

    public static final String EVENT_TEST_KEY_PRESSED = DatabaseConnectionPanel.class.getName() + ".test.pressed";

    public DatabaseConnectionPanel() {
        build();
        controller = new DatabaseConnectionController();
        setDefaults();
    }

    private void setDefaults() {
        url.setFieldValue("mercury.it.ru");
        port.setFieldValue("1521");
        sid.setFieldValue("VS");
        username.setFieldValue("inq_fskn");
        password.setFieldValue("inq_fskn");
    }

    private void build() {
        setLayout(new FlowLayout(FlowLayout.LEFT));

        inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel firstCol = new JPanel(new SpringLayout());
        url = FieldLabelPair.create("URL").width(25).addSeparate(firstCol);
        port = FieldLabelPair.create("Port").width(4).addSeparate(firstCol);
        sid = FieldLabelPair.create("SID").width(10).addSeparate(firstCol);
        SpringUtilities.makeCompactGrid(firstCol, 3, 2, 5, 5, 4, 4);
        inputPanel.add(firstCol);

        JPanel secCol = new JPanel(new SpringLayout());
        username = FieldLabelPair.create("User").addSeparate(secCol);
        password = FieldLabelPair.create("Password").addSeparate(secCol);
        SpringUtilities.makeCompactGrid(secCol, 2, 2, 5, 0, 4, 4);
        inputPanel.add(secCol);

        add(inputPanel);


        testPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.testBtn = new JButton(new AbstractAction() {

            {
                putValue(AbstractAction.NAME, "Connect");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                testBtn.setEnabled(false);
                testResultLabel.setForeground(Color.BLUE);
                testResultLabel.setText("Running test!");
                SwingWorker worker = new SwingWorker() {

                    private boolean test = false;

                    @Override
                    protected Object doInBackground() throws Exception {
                        try {
                            controller.test(getParamsMap());
                            test = true;
                        } catch (Exception e) {
                            test = false;
                        }
                        return null;
                    }

                    @Override
                    protected void done() {
                        testBtn.setEnabled(true);
                        if (test) {
                            testResultLabel.setForeground(Color.GREEN);
                            testResultLabel.setText("Connected!");
                            GuiUtils.getEventBus().post(new PanelEvent(ICdtService.CDT_LIST_UPDATED));
                        } else {
                            testResultLabel.setForeground(Color.RED);
                            testResultLabel.setText("Not connected!");
                        }
                    }
                };
                worker.execute();
            }

        });
        testPanel.add(testBtn);

        testResultLabel = new JLabel();
        testPanel.add(testResultLabel);

        add(testPanel);

        setBorder(BorderFactory.createTitledBorder("Database settings"));
    }

    public void setSuccess() {

    }

    public void setFailure() {

    }

    public Map<String, String> getParamsMap() {
        TreeMap<String, String> paramsMap = Maps.newTreeMap();
        for (FieldLabelPair pair : new FieldLabelPair[]{url, port, sid, username, password}) {
            paramsMap.put(pair.getKey(), pair.getValue());
        }
        return paramsMap;
    }


}
